package org.nfIt.service.impl;

import org.nfIt.entity.User;
import org.nfIt.mapper.UserMapper;
import org.nfIt.service.UserSerivce;
import org.nfIt.utils.Md5Util;
import org.nfIt.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Map;

/**
 * ClossName:UserServiceImpl
 * Pakage:org.nfIt.service.impl
 * Description
 *
 * @Author L
 * @Create 2024/6/3 20:37
 * @Version 2023.1.3
 */
@Service
public class UserServiceImpl implements UserSerivce {

    @Autowired
    private UserMapper userMapper;

    @Override
    public void regUserNameAndPassword(String userName, String password) {

        //加密,返回加密的password
        String md5String = Md5Util.getMD5String(password);

         userMapper.regUserNameAndPassword(userName, md5String);
    }

    @Override
    public User findByUsername(String username) {

       User byUsername = userMapper.findByUsername(username);
       return byUsername;
    }

    @Override
    public void update(User user) {
        //设置更新时间为当前时间
        user.setUpdateTime(LocalDateTime.now());
        userMapper.update(user);
    }

    /**
     * 更新头像
     * @param avatarUrl
     */
    @Override
    public void updateAvatar(String avatarUrl) {
        //从线程获取id
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
        userMapper.updateAvatar(avatarUrl,id);
    }

    @Override
    public void updatePwd(String password) {
        //从线程获取id
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
        userMapper.updatePwd(Md5Util.getMD5String(password),id);
    }
}
